﻿/************************************************************************

 This file is part of EscapeFromPrisonPlanet.

 EscapeFromPrisonPlanet is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 EscapeFromPrisonPlanet is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2012 Scott Bevin. all rights reserved

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace LudumDare25Project
{
    internal class InputManager
    {
        public enum MouseButtons
        {
            LeftButton,
            RightButton,

            Max,
        }

        public Vector2 CurrentMousePos = Vector2.Zero;

        public class MouseState
        {
            public bool LastFrame = false;
            public bool ThisFrame = false;

            public bool Pressed { get { return !LastFrame && ThisFrame; } }

            public bool Released { get { return LastFrame && !ThisFrame; } }

            public bool Down { get { return LastFrame && ThisFrame; } }

            public bool Up { get { return !LastFrame && !ThisFrame; } }
        }

        public class KeyState
        {
            public bool LastFrame = false;
            public bool ThisFrame = false;

            public bool Pressed { get { return !LastFrame && ThisFrame; } }

            public bool Released { get { return LastFrame && !ThisFrame; } }

            public bool Down { get { return LastFrame && ThisFrame; } }

            public bool Up { get { return !LastFrame && !ThisFrame; } }
        }

        public KeyState[] KeyStates = new KeyState[(int)Keys.OemClear];
        public MouseState[] MouseButtonStates = new MouseState[(int)MouseButtons.Max];

        public InputManager()
        {
            for (int i = 0; i < (int)MouseButtons.Max; ++i)
            {
                MouseButtonStates[i] = new MouseState();
            }

            for (int i = 0; i < (int)Keys.OemClear; ++i)
            {
                KeyStates[i] = new KeyState();
            }
        }

        public void Update()
        {
            CurrentMousePos = new Vector2((float)Mouse.GetState().X, (float)Mouse.GetState().Y);

            UpdateMouseButton(MouseButtonStates[(int)MouseButtons.LeftButton], Mouse.GetState().LeftButton);
            UpdateMouseButton(MouseButtonStates[(int)MouseButtons.RightButton], Mouse.GetState().RightButton);

            for (int i = 0; i < (int)Keys.OemClear; ++i)
            {
                KeyStates[i].LastFrame = KeyStates[i].ThisFrame;
                KeyStates[i].ThisFrame = Keyboard.GetState().IsKeyDown((Keys)i);
            }

#if DEBUG
            DebugDraw.Get.AddText("Mouse State: " + CurrentMousePos.ToString());
            DebugDraw.Get.AddText("Left Button: [" + MouseButtonStates[(int)MouseButtons.LeftButton].LastFrame.ToString() +
                "] [" + MouseButtonStates[(int)MouseButtons.LeftButton].ThisFrame.ToString() + "]" );
            DebugDraw.Get.AddText("Right Button: [" + MouseButtonStates[(int)MouseButtons.RightButton].LastFrame.ToString() +
                "] [" + MouseButtonStates[(int)MouseButtons.RightButton].ThisFrame.ToString() + "]" );
#endif
        }

        private void UpdateMouseButton(MouseState internalState, ButtonState xnaState)
        {
            internalState.LastFrame = internalState.ThisFrame;
            internalState.ThisFrame = (xnaState == ButtonState.Pressed);
        }
    }
}